package org.springframework.validation;

import java.util.List;

import javax.servlet.jsp.el.VariableResolver;

import org.springframework.validation.rules.ValidationRule;
import org.springframework.validation.util.RequestUtils;
import org.springframework.validation.util.WebflowVariableResolver;
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.execution.RequestContextHolder;

public class WebflowValidationManager extends ValidationManager {
	
	protected List<ValidationRule> getContextRules(Object model) {
	List<ValidationRule> flowRules = rulesContainer.getContextRules(model.getClass(), getFlowStateId());
		return flowRules;
	}
	
	protected VariableResolver getVariableResolver(Object model) {
		return new WebflowVariableResolver(getRequestContext(), model);
	}
	
	protected String getFlowStateId() {
		StringBuffer flowStateId = new StringBuffer();
		flowStateId.append(getRequestContext().getCurrentState().getOwner().getId());
		flowStateId.append(":");
		flowStateId.append(getRequestContext().getCurrentState().getId());
		String flowStateIdString = RequestUtils.removeLeadingAndTrailingSlashes(
				flowStateId.toString());
		return flowStateIdString;
	}
	
	protected RequestContext getRequestContext() {
		return RequestContextHolder.getRequestContext();
	}

}
